package com.iss.okair.db.mapper.sql;

import com.iss.common.StringTools;
import com.iss.okair.db.entity.AssetsFinance;
import com.iss.okair.db.model.AssetsFinalModle;
import org.apache.ibatis.jdbc.SQL;

public class AssetsFianlSQL {
    //动态生成查询语句
    public String find(AssetsFinalModle model){
     return new SQL(){
         {
           SELECT("*");
           FROM("assets_finance");
             if (model.getAssetsNameCn()!=null && !model.getAssetsNameCn().isEmpty()){
                 WHERE("namecn like '%"+model.getAssetsNameCn()+"%'");
             }
             if (model.getAssetsCode()!=null && !model.getAssetsCode().isEmpty()){
                 WHERE("assetsCode like '%"+model.getAssetsCode()+"%'");
             }
             if (model.getOrganization()!=null && !model.getOrganization().isEmpty()){
                 WHERE("organization like '%"+model.getOrganization()+"%'");
             }
             if (model.isAutoCycle()) {
                 WHERE("autoCycle=1");
             }
             //无意义
//             else {
//                 WHERE("autoCycle=0");
//             }
             if(model.getAssetsType2()!=null && !"-1".equals(model.getAssetsType2())){
                 WHERE("assetsType="+model.getAssetsType2());
             }
             if (model.getAssetsType1()!=null && !"-1".equals(model.getAssetsType1()) && "-1".equals(model.getAssetsType2())){
                 WHERE("assetsType in(select id from category where parentId="+model.getAssetsType1()+ ")");
         }
         }
     }.toString();
    }
    //动态生成修改语句
    public String update(AssetsFinance finance){
        return new SQL(){
            {
                UPDATE("assets_finance");
                if (!StringTools.isNullOrEmpty(finance.getNamecn()))
                    SET("namecn=#{namecn}");
                if (!StringTools.isNullOrEmpty(finance.getComment()))
                    SET("comment=#{comment}");
                if (!StringTools.isNullOrEmpty(finance.getOrganization()))
                    SET("organization=#{organization}");
                if (!StringTools.isNullOrEmpty(finance.getRefbankno()))
                    SET("refbankno=#{refbankno}");
                if (!StringTools.isNullOrEmpty(finance.getAssetscode()))
                    SET("assetscode=#{assetscode}");
                if (!StringTools.isNullOrEmpty(finance.getCreateuser()))
                    SET("createuser=#{createuser}");
                if (!StringTools.isNullOrEmpty(finance.getFinancetype()))
                    SET("financetype=#{financetype}");
                if (finance.getAssetstype()!=null&& finance.getAssetstype()!= -1)
                    SET("assetstype=#{assetstype}");
                if (finance.getCycle()!=null&& finance.getCycle()!=-1)
                    SET("cycle=#{cycle}");
                if (finance.getAutocycle()!=null){
                    SET("autocycle=#{autocycle}");
                }
                WHERE("id=#{id}");
            }
        }.toString();
    }
}
